package hibernate.web;

import hibernate.fileControler.MyException;
import hibernate.model.Aygit;
import hibernate.model.Musteriaygitlari;
import hibernate.model.Serinoversiyon;
import hibernate.model.Sirketprofil;
import hibernate.service.EmmyService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.event.ComponentSystemEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("MusteriAygitlariBean")
@Scope("session")
public class MusteriAygitlariBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;
	
	private Musteriaygitlari selectedMusAygit;	
	private List<Musteriaygitlari> musAygitlariList;
	private ArrayList <Object> aygitlar =new ArrayList<Object>(); //Bellek, Tv vb çeşitli aygıtları tutmak için...
	
	private String islemTuru;
	private transient MusteriBean musBean;
	
	@Override
	@PostConstruct	
	public void init(){
		getMusAygitlari();
		//musAygitlariList = (List<Musteriaygitlari>) emmyService.findAll(new Musteriaygitlari());
	}	
	
	@Override
	public String restart(){
		init();
		return "";
	}
	
	@Override
	public String deleteView(){
		emmyService.delete(selectedMusAygit);
		init();
		return "list";
	}

	@Override
	public String viewNew() {
		System.out.println("Müsteri aygıtları viewNew");		
		selectedMusAygit = new Musteriaygitlari();
		selectedMusAygit.setMusteri(musBean.getCurrentMusteri());
		return "";
	}

	@Override
	public String viewDetail() {/*
		List<Musteriaygitlari> valueList = (List<Musteriaygitlari>) musAygitlariTable.getValue();	
		System.out.println("table size : "+valueList.size());		
		Iterator<SimpleRowKey> keys = musAygitlariTable.getSelection().getKeys();
		
		if(keys.hasNext()){
			currentMusAygitlari= valueList.get(keys.next().intValue());
			System.out.println("dolu = "+currentMusAygitlari.getAygitSeriNo());
			return "detail";
		}else{
			System.out.println("bo�ta");
			return null;
		}*/ return"detay";
	}

	@Override
	public String SaveOrUpdate() throws MyException {
		System.out.println("save içindeyiz");
		selectedMusAygit.setAygit(( (AygitBean)emmyService.getBean(new AygitBean()) ).getSelectedAygit());
		Serinoversiyon srnver = new Serinoversiyon(selectedMusAygit, 
				( (AygitYazilimlariBean)emmyService.getBean(new AygitYazilimlariBean()))
						.getSelectedAygitYzl().getVersiyonNo()  );
		selectedMusAygit.setSerinoversiyon(srnver);
		emmyService.saveOrUpdate(selectedMusAygit);
		System.out.println("save bitti");
		clear();
		init();
		return "";
	}

	public void clear() {
		selectedMusAygit = null;
		emmyService.clearSession(new AygitBean());
		emmyService.clearSession(new SirketprofilBean());
		emmyService.clearSession(new AygitYazilimlariBean());
		emmyService.clearSession(new AygitihbarBean());
		emmyService.clearSession(new LoglarBean());
	}

	@SuppressWarnings("unchecked")
	public void getMusAygitlari(){
		try {
			musBean = (MusteriBean) emmyService.getBean(new MusteriBean());

			musAygitlariList = (List<Musteriaygitlari>) emmyService.getQuery(
					"findMusAygtKulid", "mKulid", musBean.getCurrentMusteri().getKullaniciid());

			aygitlar.clear(); // önceden doluysa iptal etmek için
			if (musAygitlariList != null) {
				for (Iterator<?> iter = musAygitlariList.iterator(); iter
						.hasNext();) {
					Aygit aygt = ((Musteriaygitlari) iter.next()).getAygit();
					getListe(aygt); // aygitlar listesini doldurma
				}
			}
		} catch (Exception e) {
			System.out.println("giriş yapılmadı.");
		}
	}
	
	public void handleSirketChange() {
		System.out.println("sirket change"); // şirket converter yapmadığın için action çalışmyor
		SirketprofilBean srfBean = (SirketprofilBean) emmyService.getBean(new SirketprofilBean());
		Sirketprofil srfprf =srfBean.getSelectedSirket();
        if(srfprf !=null && !srfprf.equals("")){
        	System.out.println("girdim");
        	System.out.println(srfprf.getFirmaid());
        	( (AygitBean)emmyService.getBean(new AygitBean()) ).getUreticiAygitlari(srfprf);
        }
        else  
        	srfBean.setSirketproList(new ArrayList<Sirketprofil>());  
    }
	
	public void handleAygitChange() {
		System.out.println("aygit change");
		AygitBean agtBean = (AygitBean) emmyService.getBean(new AygitBean());
		
		if(agtBean.getSelectedAygit() !=null && !agtBean.getSelectedAygit().equals("")){
        	( (AygitYazilimlariBean)emmyService.getBean(new AygitYazilimlariBean()) )
        			.getAygitYazilimlari(agtBean.getSelectedAygit());
        }
        else  
        	agtBean.setAygitList(new ArrayList<Aygit>());
    }  
	
	public void getListe(Aygit aygt) {

		if (aygt.getAygitTuru().equals("BELLEK")) {
			aygitlar.add(aygt.getBellekler());
		} else if (aygt.getAygitTuru().equals("BUZDOLABI")) {
			aygitlar.add(aygt.getBuzdolabi());
		} else if (aygt.getAygitTuru().equals("KLIMA")) {
			aygitlar.add(aygt.getKlima());
		} else if (aygt.getAygitTuru().equals("MP3IPOD")) {
			aygitlar.add(aygt.getMp3ipod());
		} else if (aygt.getAygitTuru().equals("TELEFON")) {
			aygitlar.add(aygt.getTelefon());
		} else if (aygt.getAygitTuru().equals("TV")) {
			aygitlar.add(aygt.getTv());			
		}
	}
	
	public void verifyMusteriAygit(ComponentSystemEvent event){
		try{
			getMusAygitlari();
			//( (AygitihbarBean)emmyService.getBean(new AygitihbarBean()) ).getMusteriIhbarlari();
		}catch(Exception e){
			System.out.println("verifyMusteriAygit hatası: "+e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public String getDetayliMusAygitlari(){		
		musAygitlariList = (List<Musteriaygitlari>) emmyService.getQuery(createQuery());
		return "";
	}
	
	@Override
	public String createQuery() {
		Musteriaygitlari musAyg  = selectedMusAygit;		
		String query = "from Musteriaygitlari";				
		
		if(musAyg == null){
			return query;
		}
		if(musAyg.getMusteri().getKullaniciid() != 0){
			if(query.indexOf("where") != -1){
				query += " and kullaniciid="+musAyg.getMusteri().getKullaniciid()+"";
				
			}
			else{
				query += " where kullaniciid="+musAyg.getMusteri().getKullaniciid()+"";			
			}
		}
		if(musAyg.getAygit().getAygitid() != 0){
			if(query.indexOf("where") != -1){
				query += " and aygitid="+musAyg.getAygit().getAygitid()+"";
				
			}
			else{
				query += " where aygitid="+musAyg.getAygit().getAygitid()+"";			
			}
		}
		return query;
	}	

	@Override
	public void getSession() {
		// TODO Auto-generated method stub
		
	}

	public Musteriaygitlari getSelectedMusAygit() {
		return selectedMusAygit;
	}

	public void setSelectedMusAygit(Musteriaygitlari selectedMusAygit) {
		this.selectedMusAygit = selectedMusAygit;
	}

	public List<Musteriaygitlari> getMusAygitlariList() {
		return musAygitlariList;
	}

	public void setMusAygitlariList(List<Musteriaygitlari> musAygitlariList) {
		this.musAygitlariList = musAygitlariList;
	}

	public MusteriBean getMusBean() {
		return musBean;
	}

	public void setMusBean(MusteriBean musBean) {
		this.musBean = musBean;
	}

	public ArrayList<Object> getAygitlar() {
		return aygitlar;
	}

	public void setAygitlar(ArrayList<Object> aygitlar) {
		this.aygitlar = aygitlar;
	}

	public String getIslemTuru() {
		return islemTuru;
	}

	public void setIslemTuru(String islemTuru) {
		this.islemTuru = islemTuru;
	}
}